/*
 *  Copyright (C) 2011 CWCLAN <www.cwclan.de>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.google.code.smon.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 *
 * @author Simon Beckstein <simon.beckstein at gmail.com>
 */
public class HtmlTextFormatter extends Formatter
{
    private boolean line=true;

    @Override
    public String format(LogRecord rec)
    {
        String color;
        if (line)
        {
            color = "#ffffff";
        }
        else
        {
            color = "#d7d7d7";
        }
        StringBuffer buf = new StringBuffer(1000);
        buf.append("<div style='background:");
        buf.append(color);
        buf.append("'>");
        buf.append("<i>");
	buf.append(calcDate(rec.getMillis()));
	buf.append("</i> ");
	// Bold any levels >= WARNING        
	if (rec.getLevel().intValue() == Level.INFO.intValue())
	{
	    buf.append("<b><font color='#ffea02'>");
	    buf.append(rec.getLevel());
	    buf.append(":</font></b> ");
	} else if (rec.getLevel().intValue() == Level.WARNING.intValue())
        {
	    buf.append("<b><font color='#FF5A00'>");
	    buf.append(rec.getLevel());
	    buf.append(":</font></b> ");
        } else if (rec.getLevel().intValue() == Level.SEVERE.intValue())
        {
	    buf.append("<b><font color='#FF0000'>");
	    buf.append(rec.getLevel());
	    buf.append(":</font></b> ");
        } else
        {
	    buf.append(rec.getLevel());
	}
        if (rec.getSourceClassName() != null) {
	    buf.append(rec.getSourceClassName());
	} else {
	    buf.append(rec.getLoggerName());
	}
        buf.append(" -> ");

        if (rec.getSourceMethodName() != null) {
	    buf.append("");
	    buf.append(rec.getSourceMethodName());
	}
        buf.append("<br />");
	buf.append(formatMessage(rec));
	buf.append("<br />\n");
        buf.append("</div>");
        line=!line;
	return buf.toString();        
    }

    private String calcDate(long millisecs)
    {
	SimpleDateFormat date_format = new SimpleDateFormat("HH:mm");
	Date resultdate = new Date(millisecs);
	return date_format.format(resultdate);
    }

    @Override
    public String getHead(Handler h)
    {
	return "<HTML>\n<HEAD>\n<h3>" + (new Date()) + "</h3>\n</HEAD>\n<BODY>\n";
    }

    @Override
    public String getTail(Handler h)
    {
	return "\n</BODY>\n</HTML>\n";
    }
}
